mpeg2ts-reader
Rust reader for MPEG2 Transport Stream data
Zero-copy access to payload data within an MPEG Transport Stream.
This crate,
- implements a low-level state machine that recognises the structural elements of Transport Stream syntax
- provides traits that you should implement to define your application-specific processing of the contained data.
Example
Dump H264 payload data as hex.
extern crate mpeg2ts_reader;
extern crate hex_slice;
use AsHex;
use demultiplex;
use packet;
use pes;
use psi;
use StreamType;
use cmp;
use env;
use File;
use Read;
// This macro invocation creates an enum called DumpFilterSwitch, encapsulating all possible ways
// that this application may handle transport stream packets. Each enum variant is just a wrapper
// around an implementation of the PacketFilter trait
packet_filter_switch!
// This macro invocation creates a type called DumpDemuxContext, which is our application-specific
// implementation of the DemuxContext trait.
demux_context!;
// When the de-multiplexing process needs to create a PacketFilter instance to handle a particular
// kind of data discovered within the Transport Stream being processed, it will send a
// FilterRequest to our application-specific implementation of the StreamConstructor trait
;
// Implement the ElementaryStreamConsumer to just dump and PTS/DTS timestamps to stdout
Performance
On my laptop (which can read sequentially from main memory at around 16GiByte/s), a microbenchmark that parses TS structure, but ignores the audio and video contained within, can process at a rate of 10 GiBytes/s (80 Gibits/s).
Real usage that actually processes the contents of the stream will of course be slower!
The conditions of the test are,
- the data is already in memory (no network/disk access)
- test dataset is larger than CPU cache
- processing is happening on a single core (no multiprocessing of the stream).
Perf shoot-out
Comparing this crate to a couple of others which you might use to read a Transport Stream -- mpeg2ts and ffmpg-sys:
The benchmarks producing the above chart data are in the shootout
folder. (If the benchmarks are giving
an unfair representation of relative performance, that's a mistake -- please raise a bug!)
Supported Transport Stream features
Not all Transport Stream features are supported yet. Here's a summary of what's available, and what's yet to come:
- Framing
- ISO/IEC 13818-1 188-byte packets
- m2ts 192-byte packets (would be nice if an external crate could support, at least)
- recovery after loss of synchronisation
- Transport Stream packet
- Fixed headers
- Adaptation field
- Program Specific Information tables
- Section syntax
- 'Multi-section' tables
- PAT - Program Association Table
- PMT - Program Mapping Table
- TSDT - Transport Stream Description Table
- Packetised Elementary Stream syntax
- PES_packet_data
- PTS/DTS
- ESCR
- ES_rate
- DSM_trick_mode
- additional_copy_info
- PES_CRC
- PES_extension
- Descriptors
- video_stream_descriptor
- audio_stream_descriptor
- hierarchy_descriptor
- registration_descriptor
- data_stream_alignment_descriptor
- target_background_grid_descriptor
- video_window_descriptor
- ca_descriptor
- iso_639_language_descriptor
- system_clock_descriptor
- multiplex_buffer_utilization_descriptor
- copyright_descriptor
- maximum_bitrate_descriptor
- private_data_indicator_descriptor
- smoothing_buffer_descriptor
- std_descriptor
- ibp_descriptor
- mpeg4_video_descriptor
- mpeg4_audio_descriptor
- iod_descriptor
- sl_descriptor
- fmc_descriptor
- external_es_id_descriptor
- muxcode_descriptor
- fmxbuffersize_descriptor
- multiplexbuffer_descriptor